package src

import (
	"fmt"
	"net/http"
	"net/url"
	"utils"
	"strconv"
	"appengine"
	"appengine/taskqueue"
	"encoding/json"
)

func init() {
	http.HandleFunc("/cron/github", cronGithubHandler)
}

func cronGithubHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	
	//Set content type 
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	
	count := utils.GetGithubCount(r)
	
	//Get list url readme 
	urlGit := "https://api.github.com/repositories?since=" + strconv.FormatInt(count, 10)
	
	fmt.Fprintln(w, urlGit)
	
    contents := utils.GetCurl(urlGit, r)
	
	
	if (contents != "") {
		var gc utils.GithubCollection
		
		err := utils.FromJson(contents, &gc)
		
		if err != nil {
			fmt.Fprintln(w, "data is not json")
			return
		}
		fmt.Fprintln(w, len(gc.Pool))
		for _, v := range gc.Pool {
			
			//Get content readme
			var readme utils.Readme
			readme.Id = v.Id 
			readme.Name = v.Name
			readme.Description = v.Description 
			readme.Url = v.Html_url			
			count = int64(v.Id)
			fmt.Fprintln(w, "Start process id", v.Id)
			
			//Put data to TaskQueue
			rm, err_rm := json.Marshal(readme)
			if err_rm != nil {
				fmt.Println("error:", err_rm)
			} 
			
			t := taskqueue.NewPOSTTask("/craw/readme",  url.Values{
    				"rm": {string(rm)},
			})
			
		    if _, err := taskqueue.Add(c, t, ""); err != nil {
		    	http.Error(w, err.Error(), http.StatusInternalServerError)
		    } 
			
			fmt.Fprintf(w, "Success get readme id %v with url %v", v.Id, v.Html_url)
			fmt.Fprintln(w, "")
		}
		
		utils.UpdateGithubCount(count, r)
	}  else {
		fmt.Fprintln(w, "content is null")
	}
	
	
}
